var express = require('express');
var router = express.Router();



var { Users } = require("../utils/model")
var {
  startEncrypt,
  getDecrypt,
  checkSession
} = require("../utils");
const { urlencoded } = require('express');
const session = require('express-session');

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});


//post注册
router.post("/register", (req, res) => {
  var body = req.body;
  console.log(body);
  var { username, password } = body;
  if (username && password) {
    Users.findOne({
      username: username
    }).then(result => {
      if (result) {
        res.send("<script>alert('用户名已经被注册,请重新注册');location.href='/register'</script>")
      } else {
        body.time = new Date();
        Users.insertMany(body)
          .then(data => {
            res.send(`<script>alert('注册成功,马上前往登录');location.href='/login?username=${startEncrypt(username)}'</script>`)
          })
      }
    })
  } else {
    res.send("<script>alert('用户名或者密码是必填字段,请重新填写');location.href='/register'</script>")
  }
})

//post的登录   
router.post("/login", (req, res) => {
  var body = req.body
  console.log(body)
  if (body.username && body.password) {

    Users.findOne({
      username: body.username
    }).then(result => {
      if (result) {
        if (result.password == body.password) {
          console.log(req.session)
          req.session.username = result.username
          req.session.logintime = new Date()
          req.session.token = startEncrypt(result.username);  //判断用户是否登录
          res.redirect("/")   //登录成功 跳转首页 重定向
        } else {
          res.send("<script>alert('用户名或者是密码错误,请重新登录');location.href='/register'</script>")
        }
      } else {
        res.send("<script>alert('用户名不存在,请重新登录');location.href='/login'</script>")
      }
    })
  } else {
    res.send("<script>alert('用户名或者是密码是必填字段,请重新填写');location.href='/login'</script>")
  }
})

//修改页面
router.post("/changepass",(req,res)=>{
  checkSession(req,res,()=>{
     var body = req.body;
     console.log(body);
     Users.findOne({
       username:body.username,
     }).then(data=>{
        if(data){
          if(data.password == body.oldpass){
             Users.updateMany({
              username:body.username,
             },{
               $set:{
                 password:body.newpass
               }
             }).then(result=>{
              // res.send("密码修改成功.....") 
              res.json({
                code:200,
                type:1,
                msg:"密码修改成功....."
              })
             })
          }else{
            // res.send("旧密码不正确...")
            res.json({
              code:200,
              type:0,
              msg:"旧密码不正确....."
            })
          }
        }else{
          // res.send("用户名不存在...")
          res.json({
            code:200,
            type:0,
            msg:"用户名不存在....."
          })
        }
     })
  })
})

router.all("/some", (req, res) => {
  res.json({
    code: 200,
    msg: "操作成功---some"
  })
})

module.exports = router;
